//公告
var catid;
setTimeout(window.autoAnimation, 5000);
//商品正文高度
$(document).ready(function() {
	goods_resize();

	function goods_resize() {
		var windowW = $(window).height();
		var less = $(".simpleheader").height() + $("#footer").height();
		if($("#callboard").css("display") == "block") {
			less = less + $("#callboard").height();
		}
		$("#goods").css("height", windowW - less);
	}
	//关闭公告
	$("#callboard .weui-icon-close").click(function() {
		$("#callboard").hide();
		goods_resize();
	})
	//	//去支付
	//	$("#topay-btn a").click(function() {
	//		if($(this).hasClass("disabled")) {
	//			mui.alert("请选择商品");
	//			return false;
	//		}
	//
	//		openNewWindows('cart.html');
	//	})
	setTimeout(function() {
		mui.preload({
			url: "cart.html",
			id: "cart.html",
		});
	}, 1000);
});
$(document).ready(function() {
	$("#save-btn a").click(function() {
		//		$(this).parents("form").submit();

	})
});
// H5 plus事件处理
function plusReady() {
	var self = plus.webview.currentWebview();
	catid = self.catid;
	//	console.log('goods_cate_id', catid);
	mui.get(base_url + 'api/goods/getCat', {
		id: catid,
	}, function(data) {
		var html = '';
		//		$('.menuli').remove();
		$.each(data.data, function(index, value) {
			console.log(value.text);
			if(index == 0) {
				var isactive = 'active';
			} else {
				var isactive = '';
			}
			html += '<li class="menuli ' + isactive + '">' +
				'<a href="#cat' + value.category_id + '">' + value.text + '</a>' +
				'</li>';
		});
		$('#goods-menu').html(html);
		$('.menuli').on('click', function() {
			$('.menuli').each(function() {
				$(this).removeClass('active');
			});
			$(this).addClass('active');
			console.log('goods_menu_click');
		});
	}, 'json');
	mui.get(base_url + 'api/goods/getPCateName', {
		id: catid
	}, function(data) {
		$('.goods_title').html(data.data.name);
	}, 'json');
	getgoods();

	function getgoods() {
		mui.get(base_url + 'api/goods/getGoods', {
			id: catid,
		}, function(data) {
			var html = '';
			//		$('.menuli').remove();
			$.each(data.data, function(index, value) {
				console.log(index);
				html += '<div id="cat' + index + '">';
				$.each(value, function(key, value) {
					console.log(value.store_count);
					if(value.store_count == "0") {
						isdisabled = 'disabled'
					} else {
						isdisabled = '';
					}
					html += '<div class="goods-item">' +
						'	<div class="fl goods-thumb show-goods-info" data-id="' + value.goods_id + '">' +
						'		<img style="width:87px;height:87px;" src="' + base_url + value.main_img + '?x-oss-process=image/resize,m_fixed,h_87,w_87">' +
						'	</div>' +
						'	<div class="fl goods-info show-goods-info" data-id="' + value.goods_id + '">' +
						'		<li class="title">' +
						'			<a href="##">' + value.goods_name + '</a>' +
						'		</li>' +
						'		<li class="price"><span>￥' + value.shop_price + '</span>元/' + value.sku + '</li>' +
						'		<li class="stock">库存' + value.store_count + '瓶 已售' + value.sales_sum + value.sku + '</li>' +
						'	</div>' +
						'	<div class="fr goods-number" data-stock="' + value.store_count + '" data-price="' + value.shop_price + '">' +
						'		<a href="javascript:;" class="weui-icon-less disabled"></a>' +
						'		<span data-id="' + value.goods_id + '" class="num">0</span>' +
						'		<a href="javascript:;" class="weui-icon-add ' + isdisabled + '"></a>' +
						'	</div>' +
						'	<div class="clearfix"></div>' +
						'</div>';

				});
				html += '</div>';

			});
			$('#total-number').html(0);
			$('#total-price').html(0);
			$('#goods-right').html(html);
			var goods = {};
			goods.num = 0;
			goods.price = 0;
			//商品数量
			$(".goods-number a").on('click', function() {
				//			console.log('123');
				if($(this).hasClass("disabled")) {
					return false;
				}
				var box = $(this).parents(".goods-number");
				var stock = parseInt(box.attr("data-stock"));
				var num = parseInt(box.find(".num").text());
				var price = parseFloat(box.attr("data-price"));

				if($(this).hasClass("weui-icon-less")) {
					if(num > 0) {
						goods.num = goods.num - 1;
						goods.price = goods.price - price;
						box.find(".num").text(num - 1);
					}
				}
				if($(this).hasClass("weui-icon-add")) {
					if(num < stock) {
						goods.num = goods.num + 1;
						goods.price = goods.price + price;
						box.find(".num").text(num + 1);
					}
				}
				goods.price = Math.round(goods.price * 10) / 10;
				num = parseInt(box.find(".num").text());
				if(num < 1) {
					box.find(".weui-icon-less").addClass("disabled");
				} else {
					box.find(".weui-icon-less").removeClass("disabled");
				}
				if(num == stock) {
					box.find(".weui-icon-add").addClass("disabled");
				} else {
					box.find(".weui-icon-add").removeClass("disabled");
				}
				$("#total-number").text(goods.num);
				$("#total-price").text(goods.price);
				if(goods.num > 0 && goods.price > 0) {
					$("#btn-submit").removeClass("disabled");
				} else {
					$("#btn-submit").addClass("disabled");
				}
				if($("#peisong-info").length > 0) {
					var peisong_price = parseFloat($("#peisong-price").text());
					var peisong_free = parseFloat($("#peisong-free").text());
					if(goods.price > peisong_free) {
						price = goods.price;
					} else {
						console.log(peisong_free);
						price = goods.price + peisong_price;
					}
					$("#pay-price").text(price);
					if(goods.price > 0) {
						$("#topay-btn a").removeClass("disabled");
					} else {
						$("#topay-btn a").addClass("disabled");
					}
				}
			})
			//查看商品详情
			$(".show-goods-info").click(function() {
				var goods_id = $(this).attr('data-id');
				$.getJSON(base_url + 'api/goods/getDeatilInfo', {
					ac: 'info',
					id: goods_id,
					//这里可以添加商品其他参数
				}, function(json) {
					if(json.title) {
						var content = '';
						content += '<h5>' + json.title + '</h5>';
						content += '<img src="' + base_url + json.thumb + '" />';
						content += '<div class="price"><label>价格</label><p><span>￥' + json.price + '</span>' + json.unit + '</p></div>';
						content += '<div class="clearfix"></div>';
						//						content += '<div class="score"><label>积分</label><p><span>' + json.score + '</span></p></div>';
						//						content += '<div class="clearfix"></div>';
						content += '<div class="weui-flex desc"><label>描述</label><p class="weui-flex__item"><span>' + json.desc + '</span></p></div>';
						content += '<div class="clearfix"></div>';
						content += '<a href="javascript:;" class="weui-icon-close2" id="close-goods-info"></a>';
						layer.open({
							className: 'goods-info-modal',
							content: content
						})
					} else {
						layer.open({
							content: json.msg,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
				})
				return false;
			})
			//关闭商品详情
			$("body").on("click", "#close-goods-info", function() {
				layer.closeAll();
			})
		}, 'json');

	}
	//提交订单
	$("#btn-submit").click(function() {
		if($(this).hasClass("disabled")) {
			mui.alert("请选择商品");
			return false;
		}
		if(plus.storage.getItem('mobile') == 0) {
			openNewWindows('login.html');
		}
		//				var goods = [];
		//		var goods = '';
		$('.num').each(function(index, value) {
			var goodsid = $(this).attr('data-id');
			var num = $(this).html();
			if(num > 0) {
				mui.post(base_url + 'api/user/savecats', {
					'goods_id': goodsid,
					'num': num,
				}, function(data) {

				}, 'json');
			}
		});
		mui.toast("加入购物车成功");
		getgoods();
		openNewWindows('cart.html?id=' + catid);
		//		goods = JSON.stringify(goods);
		//		console.log(goods);
		//		
	})
	// 设置系统状态栏背景为红色
	window.addEventListener('show', function(event) {

		init();

	});

	window.addEventListener('preshow', function(event) {
		if(plus.webview.getDisplayWebview()[0].id != "Hbulider") {
			getgoods();
			init();
		}
	});
}

function init() {
	plus.navigator.setStatusBarBackground("#27b6f0");
	//异步预加载
	setTimeout(function() {
		mui.preload({
			url: "cart.html?id=" + catid,
			id: "cart.html?id=" + catid,
		});
	}, 1000);
	setTimeout(function() {
		mui.preload({
			url: "pay.html?id=" + catid,
			id: "pay.html?id=" + catid,
		});
	}, 1000);
	$('#gocart').on('click', function() {
		is_login();
		if(plus.storage.getItem('mobile') != 0) {
			openNewWindows('cart.html?id=' + catid);
			return false;
		} else {
			console.log('gologin2');
			openNewWindows('login.html');
		}

	});
	$('#gogoods').on('click', function() {
		openNewWindows('goods.html?id=' + catid);
	});

}
mui.ready(function() {})
if(window.plus) {
	plusReady();
} else {
	document.addEventListener("plusready", plusReady, false);
}